<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>高校宿舍管理系统-宿舍分配信息</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="shortcut icon" href="#"/>
    <link rel="stylesheet" href="../layui/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../layui/css/public.css" media="all">
    <style type="text/css">
        .layui-form-item {
            display: inline-block;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">专业</label>
                            <div class="layui-input-inline">
                                <input type="text" name="studept" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">班级</label>
                            <div class="layui-input-inline">
                                <input type="text" name="stuclass" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i>查 询
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <!-- 表格头部工具栏 -->
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <!--                <button class="layui-btn layui-btn-sm data-add-btn" lay-event="add"><i-->
                <!--                        class="layui-icon layui-icon-add-1"></i>添加-->
                <!--                </button>-->
                <button class="layui-btn layui-btn-sm data-add-btn" lay-event="export"><i
                        class="layui-icon layui-icon-file"></i>导出
                </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <!-- 行内按钮 -->
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs data-count-edit" lay-event="edit"><i
                    class="layui-icon layui-icon-edit"></i>编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete"><i
                    class="layui-icon layui-icon-delete"></i>删除</a>
        </script>

        <!-- 添加和修改窗口 -->
        <div style="display: none;padding: 5px" id="addOrUpdateWindow">
            <form class="layui-form" style="width:90%;" id="dataFrm" lay-filter="dataFrm">
                <div class="layui-form-item">
                    <label class="layui-form-label">宿舍区</label>
                    <div class="layui-input-block">
                        <input type="hidden" name="brcode">
                        <input type="text" name="brarea" lay-verify="required" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">楼栋</label>
                    <div class="layui-input-block">
                        <input type="text" name="brbid" lay-verify="required" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">寝室</label>
                    <div class="layui-input-block">
                        <input type="text" name="brrid" lay-verify="required" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">床位号</label>
                    <div class="layui-input-block">
                        <input type="text" name="bed" lay-verify="required" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">姓名</label>
                    <div class="layui-input-block">
                        <input type="text" name="stuname" lay-verify="required" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">学号</label>
                    <div class="layui-input-block">
                        <input type="text" name="stuid" lay-verify="required" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item layui-row layui-col-xs12">
                    <div class="layui-input-block" style="text-align: center;">
                        <button type="button" class="layui-btn" lay-submit lay-filter="doSubmit"
                                style="margin-right: 20px">
                            <span class="layui-icon layui-icon-add-1"></span>提交
                        </button>
                        <button type="reset" class="layui-btn layui-btn-warm">
                            <span class="layui-icon layui-icon-refresh-1"></span>重置
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script src="../layui/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'layer'], function () {
        var $ = layui.jquery,
            form = layui.form,
            layer = layui.layer,
            table = layui.table;

        var tableIns = table.render({
            elem: '#currentTableId',
            url: '/teacher/allocation/list',
            toolbar: '#toolbarDemo',
            cols: [
                [
                    {type: "checkbox", width: 50},
                    {field: 'brcode', title: '宿舍编号', hide: true},
                    {field: 'brarea', width: 100, title: '宿舍区', align: "center"},
                    {field: 'brbid', width: 90, title: '楼栋', align: "center"},
                    {field: 'brrid', width: 100, title: '寝室', align: "center"},
                    {field: 'stucollege', minWidth: 150, title: '学院', align: "center"},
                    {field: 'studept', title: '专业', width: 150, align: "center"},
                    {field: 'stuclass', width: 120, title: '班级', align: "center"},
                    {field: 'stuname', width: 150, title: '姓名'},
                    {field: 'stuid', width: 150, title: '学号', align: "center"},
                    {field: 'bed', width: 100, title: '床位号', align: "center"}
                    // {title: '操作', width: 190, toolbar: '#currentTableBar', align: "center"}
                ]
            ],
            limits: [5, 10, 20, 30, 50],
            limit: 5,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            console.log(data);
            //执行搜索重载
            tableIns.reload({
                where: data.field,
                page: {
                    curr: 1
                }
            });
            return false;
        });


        //表格头部工具栏触发事件
        table.on('toolbar(currentTableFilter)', function (obj) {
            switch (obj.event) {
                case 'add':
                    openAddWindow();// 打开添加窗口
                    break;
                case 'export':
                    exportToExcel();// 导出数据
                    break;
            }
            ;
        });

        var url;
        var windowIndex;//窗口的索引

        // 打开添加窗口
        function openAddWindow() {
            windowIndex = layer.open({
                type: 1,
                title: "添加宿舍分配信息",
                area: ["700px", "280px"],
                content: $("#addOrUpdateWindow"),
                success: function () {
                    $("#dataFrm")[0].reset();
                    url = "/teacher/allocation/add";
                }
            });
        }

        // 导出数据
        function exportToExcel() {
            window.location.href = "/teacher/allocation/export.action";
        }

        // 监听添加窗口中表单提交事件
        form.on("submit(doSubmit)", function (data) {
            $.post(url, data.field, function (result) {
                if (result.success) {
                    //刷新数据表格
                    tableIns.reload();
                    //关闭窗口
                    layer.close(windowIndex);
                }
                //提示信息
                layer.msg(result.msg);
            }, "json");
            //禁止页面刷新
            return false;
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        // 监听行工具栏事件
        table.on('tool(currentTableFilter)', function (obj) {
            switch (obj.event) {
                // 修改
                case 'edit':
                    openUpdateWindow(obj.data);
                    break;
                // 删除
                case 'delete':
                    deleteById(obj.data);
                    break;
            }
        });

        /**
         * 打开修改窗口
         * @param data 当前行的数据
         */
        function openUpdateWindow(data) {
            windowIndex = layer.open({
                type: 1,
                title: "修改宿舍分配信息",
                area: ["700px", "280px"],
                content: $("#addOrUpdateWindow"),
                success: function () {
                    // 表单数据回显
                    form.val("dataFrm", data);
                    url = "/teacher/allocation/update";
                }
            });
        }

        /**
         * 删除宿舍分配信息
         * @param data
         */
        function deleteById(data) {
            layer.confirm('确定删除吗？', {icon: 3, title: '提示'}, function (index) {
                //发送ajax请求进行删除
                $.post("/teacher/allocation/delete", {"stuid": data.stuid}, function (result) {
                    if (result.success) {
                        tableIns.reload();
                    }
                    layer.msg(result.msg);
                }, "json");
                layer.close(index);
            });
        }
    });
</script>

</body>
</html>